package com.sky.controller.user;

import com.sky.dto.*;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.UserOrderService;
import com.sky.service.impl.UserOrderServiceImpl;
import com.sky.vo.HistoryOrderVO;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/user/order")
@Slf4j
public class UserOrderController {

    @Autowired
    private UserOrderServiceImpl userOrderService;

    @GetMapping("/historyOrders")
    public Result historyOrders(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("查询历史订单：{}", ordersPageQueryDTO);
        PageResult result = userOrderService.historyOrders(ordersPageQueryDTO);
        return Result.success(result);
    }

    @GetMapping("/orderDetail/{id}")
    public Result orderDetail(@PathVariable Long id) {
        log.info("查询订单为id的详情：{}",id);
        HistoryOrderVO orderDetail = userOrderService.orderDetail(id);
        return Result.success(orderDetail);
    }

    @PostMapping("/submit")
    public Result submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) throws IOException {
        log.info("提交订单：{}",ordersSubmitDTO);
        OrderSubmitVO submit = userOrderService.submit(ordersSubmitDTO);
        return Result.success(submit);
    }

    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable Long id) {
        log.info("取消订单：{}",id);
        userOrderService.cancel(id);
        return Result.success();
    }

    @PutMapping("payment")
    public Result payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) {
        log.info("支付订单{}",ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = userOrderService.payment(ordersPaymentDTO);
        return Result.success(orderPaymentVO);
    }

}
